﻿using Acme.BookStore.Books;
using Acme.BookStore.Goodss;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    public class ProductlAppService : ApplicationService, IProductService


    {
        private readonly IRepository<Goods, int> _goods;
        private readonly IRepository<CommodityType, int> _commodityTypes;
        private readonly IRepository<FileImage, int> _fileImages;


        public ProductlAppService(IRepository<Goods, int> goods, IRepository<CommodityType, int> commodityTypes,IRepository<FileImage,int> fileImages)
        {
            _goods = goods;
            _commodityTypes = commodityTypes;
            _fileImages = fileImages;
        }
        [HttpGet]
        public PagedResultDto<GoodsDto> Getlist(queryDto input)
        {
            var query = from g in _goods
                        join c in _commodityTypes
                        on g.productTypeId equals c.Id
                        join f in _fileImages
                        on g.productSpeciId equals f.imageSId
                        select new GoodsDto()
                        {
                            Id = g.Id,
                            productTypeId = c.Id,
                            typeName = c.typeName,
                            productAttrName = g.productAttrName,
                            productDetails = g.productDetails,
                            productDisplay = g.productDisplay,
                            productName = g.productName,
                            productSalePrice = g.productSalePrice,
                            productSPromotion = g.productSPromotion,
                            productState = g.productState,
                            productSubtitle = g.productSubtitle,
                            productWeight = g.productWeight,
                            productSpeciId = f.imageSId,
                            IamgeUrl=f.IamgeUrl
                        };
            var list = query.WhereIf(!string.IsNullOrEmpty(input.productName), s => s.productName.Contains(input.productName))
                       .WhereIf(input.productTypeId>0,s=>s.productTypeId== input.productTypeId)
                       .WhereIf(input.productDisplay>0,s=>s.productDisplay==input.productDisplay)
                       .ToList();
            var totaCount = list.Count();
            return new PagedResultDto<GoodsDto>(totaCount,list);
        }
        [HttpGet]
        public GoodsDto State(int id)
        {

            Goods detail = _goods.FirstOrDefault(x => x.Id == id);
            if (detail.productDisplay==1)
            {
                detail.productDisplay = 2;
            }
            else if (detail.productDisplay == 2)
            {
                detail.productDisplay = 1;
            }
            return ObjectMapper.Map<Goods, GoodsDto>(detail);
        }
    }
}
//using Abp.Application.Services;
//using Abp.Application.Services.Dto;
//using Acme.BookStore.Books;
//using Acme.BookStore.Bookss;
//using Acme.BookStore.FileImagess;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using Volo.Abp.Domain.Repositories;

//namespace Acme.BookStore
//{
//    public class ImagesAppService : ApplicationService, IImageAppService
//    {
//        private readonly IRepository<FileImage, int> _fileImage;
//        public ImagesAppService(IRepository<FileImage, int> fileImages)
//        {
//            _fileImage = fileImages;
//        }
//        public int CreateImg(CreateUpdateFileImageDto createUpdate)
//        {
//            List<FileImage> list = new List<FileImage>();
//            foreach (var item in createUpdate.IamgeUrl)
//            {
//                list.Add(new FileImage { IamgeUrl = item.ToString(), imageCategroy = createUpdate.imageCategroy, imageSId = createUpdate.imageSId });
//            }
//            _fileImage.InsertManyAsync(list);
//            return 1;
//        }

//        public int DeleteImage(List<int> ids)
//        {
//            foreach (var id in ids)
//            {
//                _fileImage.DeleteAsync(id);
//            }
//            return 1;
//        }

//        public PagedResultDto<FileImageDto> GetImage(ImageListDto input)
//        {
//            var geting = from a in _fileImage
//                         select new FileImageDto
//                         {
//                             IamgeUrl = a.IamgeUrl,
//                             imageSId = a.imageSId,
//                             imageCategroy = a.imageCategroy
//                         };
//            var list = geting.WhereIf(input.imageSId > 0, s => s.imageSId == input.imageSId).ToList();
//            var totalcounnt = list.Count;
//            return new Abp.Application.Services.Dto.PagedResultDto<FileImageDto>(totalcounnt, list);
//        }
//    }
//}

